package com.bao.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.sql.DataSource;
import java.util.zip.DataFormatException;


@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserDetailsService userDetailsService;

    /*登陆成功处理器*/
    @Autowired
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;
    /*失败的处理方案*/
    @Autowired
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;
    /*用户未登录*/
    @Autowired
    private MyAuthenticationEntryPoint myAuthenticationEntryPoint;
    /*权限不足处理器*/
    @Autowired
    private MyAccessDeniedHandler myAccessDeniedHandler;
    /*注销的处理器*/
    @Autowired
    private MyLogoutHandler myLogoutHandler;
    /*注销成功的处理器*/
    @Autowired
    private MyLogoutSuccessHandler myLogoutSuccessHandler;


    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        /*动态授权*/
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }


    @Override
    public void configure(org.springframework.security.config.annotation.web.builders.WebSecurity web) throws Exception {
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                //需要放行的url在这里配置,必须要放行/login和/login.html,不然会报错
                .antMatchers("/login","/login.html").permitAll()
                //
                .antMatchers("/users","/roles","/system/entity/deleEntity","/system/effect/deleEffet")
                //.hasAuthority("ROLE_user")
                .hasAnyAuthority("ROLE_admin","ROLE_expert")
                .antMatchers("/menus","/others")
                //.hasRole("admin")
                .hasAnyRole("admin")
                .antMatchers("/system/department/findDeptAndCount","/system/user/deleUser")
                //.hasRole("admin")
                .hasAnyRole("admin")
                .anyRequest()
                .authenticated()
                .and()
                /*---------------------------------------------注销操作------------------------------------------------*/
                /*允许注销操作*/
                .logout().permitAll()
                /*添加注销操作处理器*/
                .addLogoutHandler(myLogoutHandler)
                /*添加注销成功处理器*/
                .logoutSuccessHandler(myLogoutSuccessHandler)
                /*注销删除cookies*/
                .deleteCookies("JSESSIONID")
                .and()
                // 设置登陆页
                .formLogin().loginPage("/login.html")
                //登录表单form中action的地址，也就是处理认证请求的路径
                .loginProcessingUrl("/login")
                //登录表单form中用户名输入框input的name名，不修改的话默认是username
                .usernameParameter("username")
                //登录表单form中密码输入框input的name名，不修改的话默认是password
                .passwordParameter("password")
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenticationFailureHandler)
                .and()
                .exceptionHandling()
                /*未登录访问异常*/
                .authenticationEntryPoint(myAuthenticationEntryPoint)
                /*权限不足异常*/
                .accessDeniedHandler(myAccessDeniedHandler);
        /*关闭跨域攻击*/
        http.csrf().disable();

    }
}
